﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkitApp.Models;
using CommunityToolkitApp.Views;

namespace CommunityToolkitApp.ViewModels
{
    public partial class LoginWindowViewModel : ObservableRecipient
    {
        public LoginWindowViewModel()
        {
            IsActive = true;//如果不为true  接收不到消息
        }

        [ObservableProperty]
        string receiveContent = "未获取到信息";

        [RelayCommand]
        private void TestClick()
        {
            MainWindow mainWindow = new MainWindow();
            mainWindow.Show();
        }

        protected override void OnActivated()
        {
            base.OnActivated();
            Messenger.Register<LoginWindowViewModel, MessageObject<string>>(this, (vm, m) => Receive(m));
        }

        protected override void OnDeactivated()
        {
            base.OnDeactivated();
            Messenger.Unregister<MessageObject<string>>(this);
        }

        private void Receive(MessageObject<string> message)
        {
            ReceiveContent = message.Data;
        }
    }
}
